package manage.p.view;

import manage.p.bean.Customer;
import manage.p.service.CustomerList;
import manage.p.util.CMUtility;

/**
 * @ClassName CustomerView
 * @Description 主菜单负责和用户进行交互
 * @Author wuhn Email:1845979768@qq.com
 * @Date 2021/8/11 21:34
 * @Version 1.0
 */
public class CustomerView {
    private CustomerList customerList = new CustomerList(10);

    public CustomerView() {
        Customer customer = new Customer("王涛", '男', 23, "123456", "1845979768@qq.com");
        customerList.addCustomer(customer);
    }

    public void enterMainMenu() {
        boolean isflag = true;
        while (isflag) {
            System.out.println("-------------------客户信息管理软件-------------------");
            System.out.println("                    1.添加客户");
            System.out.println("                    2.修改客户");
            System.out.println("                    3.删除客户");
            System.out.println("                    4.客户列表");
            System.out.println("                    5.退出");
            System.out.print("         请选择(1-5):");
            char menu = CMUtility.readMenuSelection();
            switch (menu) {
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomers();
                    break;
                case '5':
//                    System.out.println("退出");
                    System.out.println("确认是否退出(Y/N):");
                    char isExit = CMUtility.readConfirmSelection();
                    if (isExit == 'Y') {
                        isflag = false;
                    }
                    break;
            }
            //isflag=false;
        }
    }

    /**
     * 添加客户的操作
     */
    private void addNewCustomer() {
//        System.out.println("添加客户的操作");
        System.out.println("-------------------添加客户-------------------");
        System.out.print("姓名:");
        String name = CMUtility.readString(4);
        System.out.print("性别:");
        char gender = CMUtility.readChar();
        System.out.print("年龄:");
        int age = CMUtility.readInt();
        System.out.print("电话:");
        String phone = CMUtility.readString(13);
        System.out.print("邮箱:");
        String email = CMUtility.readString(30);

        Customer customer = new Customer(name, gender, age, phone, email);
        boolean flag = customerList.addCustomer(customer);
        if (flag) {
            System.out.println("添加完成");
        } else {
            System.out.println("添加失败");
        }
    }

    /**
     * 修改客户的操作
     */
    private void modifyCustomer() {
//        System.out.println("修改客户的操作");
        int num;
        Customer cust;
        System.out.println("-------------------修改客户-------------------");
        while (true) {
            System.out.print("输入待修改用户编号(-1退出):");
            num = CMUtility.readInt();
            if (num == -1) {
                return;
            }
            cust = customerList.getCustomers(num - 1);
            if (cust == null) {
                System.out.println("无法找到指定客户!");
            } else {//找到相应客户
                break;
            }
        }
        //修改客户信息
        System.out.print("姓名(" + cust.getName() + "):");
        String name = CMUtility.readString(10, cust.getName());
        System.out.print("性别(" + cust.getGender() + "):");
        char gender = CMUtility.readChar(cust.getGender());
        System.out.print("年龄(" + cust.getAge() + "):");
        int age = CMUtility.readInt(cust.getAge());
        System.out.print("电话(" + cust.getPhone() + "):");
        String phone = CMUtility.readString(13, cust.getPhone());
        System.out.print("邮箱(" + cust.getEmail() + "):");
        String email = CMUtility.readString(30, cust.getEmail());

        Customer newCustomer = new Customer(name, gender, age, phone, email);
        boolean isReplaced = customerList.replaceCustomer(num - 1, newCustomer);
        if (isReplaced) {
            System.out.println("-------------------修改成功-------------------");
        } else {
            System.out.println("-------------------修改失败-------------------");
        }
    }

    /**
     * 删除客户的操作
     */
    private void deleteCustomer() {
//        System.out.println("删除客户的操作");
        int num;
        System.out.println("-------------------删除客户-------------------");
        while (true) {
            System.out.print("选择待删除客户编号:");
            num = CMUtility.readInt();
            if (num == -1) {
                return;
            }
            Customer cust = customerList.getCustomers(num - 1);
            if (cust == null) {
                System.out.println("无法找到指定客户!");
            } else {//找到相应客户
                break;
            }
        }

        System.out.println("是否确认删除(Y/N):");
        char isDelete = CMUtility.readChar();
        if (isDelete == 'Y') {
            customerList.deleteCustomer(num - 1);
        }
        /**
         * 显示所有客户信息
         */
    }
    private void listAllCustomers() {
            System.out.println("显示所有客户信息");
            System.out.println("--------------------------客户列表--------------------------");
            if (customerList.getTotal() == 0) {
                System.out.println("没有用户记录");
            } else {
                System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
                customerList.getAllCustomer();
                Customer[] custs = customerList.getAllCustomer();
                for (int i = 0; i < custs.length; i++) {
                    Customer cust = custs[i];
                    System.out.println((i + 1) + "\t" + cust.getName() + "\t" + cust.getGender()
                            + "\t" + cust.getAge() + "\t" + cust.getPhone() + "\t" + cust.getEmail());
                }
            }

            System.out.println("------------------------客户列表完成--------------------------");
        }


    public static void main(String[] args) {
        CustomerView view = new CustomerView();
        view.enterMainMenu();
    }
}
